package com.feng.dt.service.impl;

import com.feng.common.db.querydsl.FetchResponse;
import com.feng.common.db.querydsl.FetchResponseBuilder;
import com.feng.common.web.Range;
import com.feng.dt.repository.SysMenuRepository;
import com.feng.dt.service.SysMenuService;
import com.feng.profile.model.entity.QSysMenu;
import com.feng.profile.model.entity.SysMenu;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Map;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @PersistenceContext
    private EntityManager em;

    @Autowired
    private SysMenuRepository sysMenuRepository;

    @Override
    public FetchResponse<SysMenu> getAllByPage(Range range) {
        QSysMenu qSysMenu = QSysMenu.sysMenu;
        JPAQuery<SysMenu> query = new JPAQueryFactory(em).select(qSysMenu).from(qSysMenu).orderBy(qSysMenu.menuId.asc()).fetchAll();
        return new FetchResponseBuilder<SysMenu>().range(range).build(query);
    }

    @Override
    public FetchResponse<Map<String, Object>> getAllAsMap(Range range) {
        QSysMenu qSysMenu = QSysMenu.sysMenu;
        JPAQuery<Tuple> query = new JPAQueryFactory(em).select(qSysMenu.menuId, qSysMenu.name).from(qSysMenu).orderBy(qSysMenu.menuId.asc()).fetchAll();
        return new FetchResponseBuilder<Map<String, Object>>().range(range).buildAsMap(query,qSysMenu.menuId,qSysMenu.name);
    }


}
